<?php

/**
 * Name: 值班领导记录模型层
 * User: Silent
 * Date: 2025-11-07
 * Time: 15:00:00
 */

namespace App\Models\Checkin;

use App\Models\Config\Employee;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class DutySupervisor extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'duty_supervisors';

    protected $guarded = [];

    protected $casts = [
        'duty_date' => 'date:Y-m-d',
        'is_notified' => 'boolean',
        'notified_at' => 'datetime:Y-m-d H:i:s',
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
    ];

    protected static $logName = 'duty_supervisors';

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 关联主管（员工）
     */
    public function supervisor()
    {
        return $this->belongsTo(Employee::class, 'supervisor_id');
    }

    /**
     * 检查是否已通知
     */
    public function isNotified()
    {
        return $this->is_notified;
    }

    /**
     * 标记为已通知
     */
    public function markAsNotified()
    {
        $this->update([
            'is_notified' => true,
            'notified_at' => now()
        ]);
    }

    /**
     * 获取当天的值班领导
     */
    public static function getTodayDutySupervisor()
    {
        return self::where('duty_date', today())
                  ->with('supervisor')
                  ->first();
    }

    /**
     * 获取指定日期的值班领导
     */
    public static function getDutySupervisorByDate($date)
    {
        return self::where('duty_date', $date)
                  ->with('supervisor')
                  ->first();
    }
}

